package com.cloud.eureka.configure;

import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author: zoub
 * @date 2020/11/11 18:16
 **/
@Component
@Slf4j
public class EurekaEventListener {


    /**
     * EurekaServer启动事件
     *
     * @param startedEvent
     */
    @EventListener
    public void Listener(EurekaServerStartedEvent startedEvent) {
        log.info(">>>>>>>>>>>>>>>服务注册中心已完全启用....");
    }

    /**
     * 注册中心可用事件
     *
     * @param availableEvent
     */
    @EventListener
    public void Listener(EurekaRegistryAvailableEvent availableEvent) {
        log.info(">>>>>>>>>>>>>>>服务注册中心已可用....");

    }

    /**
     * 服务注册监听事件
     *
     * @param registeredEvent
     */
    @EventListener
    public void Listener(EurekaInstanceRegisteredEvent registeredEvent) {
        InstanceInfo instanceInfo = registeredEvent.getInstanceInfo();
        log.info(">>>>>>>>>>>>>>>【{}】服务注册上线 ip: {}", instanceInfo.getAppName(), instanceInfo.getIPAddr());
    }


    /**
     * 服务续约事件
     *
     * @param renewedEvent
     */
    @EventListener
    public void Listener(EurekaInstanceRenewedEvent renewedEvent) {
        InstanceInfo instanceInfo = renewedEvent.getInstanceInfo();
        log.info(">>>>>>>>>>>>>>>【{}】服务向注册中心发出了续约 ip:{}", instanceInfo.getAppName(), instanceInfo.getIPAddr());

    }


    /**
     * 服务下线事件
     *
     * @param canceledEvent
     */
    @EventListener
    public void Listener(EurekaInstanceCanceledEvent canceledEvent) {
        log.warn(">>>>>>>>>>>>>>>【{}】服务已下线", canceledEvent.getServerId());
    }

}
